package com.pifuke.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.o.app.adapter.IItemView;
import cn.o.app.adapter.OAdapter;
import cn.o.app.adapter.OItemView;
import cn.o.app.ui.OImageView;
import cn.o.app.ui.photo.PhotoActivity;
import cn.o.app.ui.photo.PhotoExtra;
import cn.o.app.util.OUtil;
import cn.skinapp.R;

import com.lidroid.xutils.BitmapUtils;
import com.smiier.skin.net.entity.Question;
import com.smiier.skin.net.entity.QuestionAnswer;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.util.StringArrayUtil;
import com.smiier.skin.utils.CommonUtility;

public class QuestionAdapter extends OAdapter<QuestionAnswer> {

	protected BitmapUtils mBitmapUtils;

	@Override
	protected void onContainerChanged() {
		mBitmapUtils = new BitmapUtils(getContext());
	}

	@Override
	public IItemView<QuestionAnswer> getItemView() {
		return new QuestionItemView(getContext());
	}

	class QuestionItemView extends OItemView<QuestionAnswer> {
		protected TextView mContentText;
		protected TextView mProfileText;
		protected TextView mXuanShangText;
		protected TextView mXuanShangStatusText;
		protected TextView mDateText;
		protected HorizontalScrollView mImgsScroll;
		protected LinearLayout mImgsLayout;
		protected View mAnswerTopLine;
		protected LinearLayout mAnswerDetailLayout;
		protected OImageView mDoctorImage;
		protected TextView mZhenDuanText;
		protected TextView mDoctorNameText;
		protected TextView mDoctorTitleText;

		public QuestionItemView(Context context) {
			super(context);
		}

		@Override
		public void onCreate() {
			this.setContentView(R.layout.item_question);

			mContentText = this.findViewById(R.id.text_content, TextView.class);
			mProfileText = this.findViewById(R.id.text_profile, TextView.class);
			mXuanShangText = this.findViewById(R.id.text_xuanshang_num,
					TextView.class);
			mXuanShangStatusText = this.findViewById(
					R.id.text_xuanshang_status, TextView.class);
			mDateText = this.findViewById(R.id.text_date, TextView.class);
			mImgsScroll = this.findViewById(R.id.hsv_imgs,
					HorizontalScrollView.class);
			mImgsLayout = this.findViewById(R.id.ll_imgs, LinearLayout.class);
			mAnswerTopLine = this.findViewById(R.id.ll_detail_line, View.class);
			mAnswerDetailLayout = this.findViewById(R.id.ll_detail,
					LinearLayout.class);
			mDoctorImage = this.findViewById(R.id.img_doctor_icon,
					OImageView.class);
			mZhenDuanText = this.findViewById(R.id.text_zhenduan,
					TextView.class);
			mDoctorNameText = this.findViewById(R.id.text_doctor_name,
					TextView.class);
			mDoctorTitleText = this.findViewById(
					R.id.text_doctor_title_hospital, TextView.class);
		}

		@Override
		public void onResume() {
			StringBuilder sbProfile = new StringBuilder();
			String sex = GlobalSettings.getSex(mDataProvider.Question.Sex);
			if (!OUtil.isEmpty(sex)) {
				sbProfile.append(sex);
			}
			if (mDataProvider.Question.Age != 0) {
				sbProfile.append("  |  ").append(mDataProvider.Question.Age)
						.append("岁");
			}
			try {
				mContentText.setText(mDataProvider.Question.Content.replace(
						"\n", ""));
			} catch (Exception e) {
				mContentText.setText(mDataProvider.Question.Content);
			}
			if (mDataProvider.Question.CreateTime != null)

				mDateText.setText(CommonUtility.getTimeDiff(
						mDataProvider.Question.CreateTime.toString(), 5, 10));

			mXuanShangStatusText.setVisibility(View.VISIBLE);
			int status = mDataProvider.Question.Status;
			if (status == Question.STATUS_PRE_ANSWER) {
				mXuanShangStatusText.setText("待接单");
				mXuanShangStatusText.setTextColor(0xffdaad29);
			} else if (status == Question.STATUS_ANSWERING) {
				mXuanShangStatusText.setText("待解答");
				mXuanShangStatusText.setTextColor(0xffdaad29);
			} else if (status == Question.STATUS_ANSWERED) {
				mXuanShangStatusText.setText("已完成");
				mXuanShangStatusText.setTextColor(Color.GRAY);
			} else {
				mXuanShangStatusText.setText("待评价");
				mXuanShangStatusText.setTextColor(Color.GRAY);
				// mXuanShangStatusText.setVisibility(View.GONE);
			}

			boolean isTextXuanShangVLine = mXuanShangStatusText.getVisibility() == View.VISIBLE;
			boolean isTextProfileVLine = false;// 是否需要添加"  |  "
			if (mDataProvider.Question.Is_Cost == GlobalSettings.IS_TRUE) {
				isTextProfileVLine = true;
				mXuanShangText.setVisibility(View.VISIBLE);
				mXuanShangText.setText("悬赏:￥" + mDataProvider.Question.Cost
						+ (isTextXuanShangVLine ? "  |  " : ""));
			} else {
				isTextProfileVLine = isTextXuanShangVLine;
				mXuanShangText.setVisibility(View.GONE);
			}

			if (isTextProfileVLine) {
				sbProfile.append("  |  ");
			}
			mProfileText.setText(sbProfile);

			if (CommonUtility.isDoctor() && CommonUtility.isCert()) {
				ArrayList<String> pics = mDataProvider.Question.Pic;
				mImgsLayout.removeAllViews();
				if (pics.size() > 0) {
					mImgsScroll.setVisibility(View.VISIBLE);
					for (int i = 0; i < pics.size(); i++) {
						OImageView layout = new OImageView(getContext());
						layout.setIsRoundRect(true);
						layout.setScaleType(ScaleType.CENTER_CROP);
						LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
								(int) OUtil.dp2px(getContext(), 70),
								(int) OUtil.dp2px(getContext(), 70));
						layoutParams.setMargins(0, 0,
								(int) OUtil.dp2px(getContext(), 8), 0);
						layout.setLayoutParams(layoutParams);
						layout.setTag(i);
						layout.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View v) {
								Context context = getContext();
								ArrayList<String> paths = new ArrayList<String>();

								for (int i = 0, size = mDataProvider.Question.Pic
										.size(); i < size; i++) {
									paths.add(GlobalSettings.SERVER_IMG_URL
											+ mDataProvider.Question.Pic.get(i)
											+ GlobalSettings.IMG_SRC);
								}
								int index = (Integer) v.getTag();
								PhotoExtra extra = new PhotoExtra();
								extra.setDisplayedIndex(index);
								extra.setPhotos(paths);
								Intent intent = new Intent(context,
										PhotoActivity.class);
								extra.putTo(intent);
								context.startActivity(intent);
							}
						});
						String path = GlobalSettings.SERVER_IMG_URL
								+ pics.get(i) + GlobalSettings.IMG_THUMB;
						mBitmapUtils.display(layout, path);
						// CommonUtility.displayHeadImage(layout, mBitmapUtils,
						// path, getContext().getResources(),
						// mDataProvider.Question.Sex);
					}
				} else {
					mImgsScroll.setVisibility(View.GONE);
				}
			} else {
				mImgsScroll.setVisibility(View.GONE);
			}

			if (mDataProvider.Question.Status != Question.STATUS_PRE_ANSWER) {
				if (!OUtil.isEmpty(mDataProvider.Doctor_User.Name)) {
					mAnswerTopLine.setVisibility(View.VISIBLE);
					mAnswerDetailLayout.setVisibility(View.VISIBLE);
					String iconPath = mDataProvider.Doctor_User.Pic;
					if (iconPath.lastIndexOf(".") == -1) {
						iconPath += GlobalSettings.IMG_THUMB;
					}
					iconPath = GlobalSettings.SERVER_IMG_URL + iconPath;
					// mBitmapUtils.display(mDoctorImage, iconPath,
					// new OBitmapLoadCallBack<View>());
					CommonUtility.displayHeadImage(mDoctorImage, mBitmapUtils,
							iconPath, getContext().getResources(),
							mDataProvider.Question.Sex);
					mDoctorNameText.setText(mDataProvider.Doctor_User.Name);
					StringBuilder sb = new StringBuilder();
					String tmp = StringArrayUtil
							.toString(mDataProvider.Doctor_User.Professional);
					if (tmp != null && !tmp.trim().equals("")) {
						sb.append(" | ");
						sb.append(tmp);
					}
					if (mDataProvider.Doctor_User.Hospital != null
							&& !mDataProvider.Doctor_User.Hospital.trim()
									.equals("")) {
						sb.append(" | ");
						sb.append(mDataProvider.Doctor_User.Hospital);
					}
					mDoctorTitleText.setText(sb);
					if (!OUtil.isEmpty(mDataProvider.Answer.Malady)) {
						mZhenDuanText.setText("\""
								+ mDataProvider.Answer.Malady + "\"");
					} else {
						mZhenDuanText.setText("等待输出诊断结果...");
					}
				} else {
					mAnswerTopLine.setVisibility(View.GONE);
					mAnswerDetailLayout.setVisibility(View.GONE);
				}
			} else {
				mAnswerTopLine.setVisibility(View.GONE);
				mAnswerDetailLayout.setVisibility(View.GONE);
			}
		}
	}

}